package com.lishangshan.oas_system.controller.inform;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lishangshan.oas_system.model.dao.inform.NoticeMapper;
import com.lishangshan.oas_system.model.dao.informdao.InformDao;
import com.lishangshan.oas_system.model.dao.informdao.InformRelationDao;
import com.lishangshan.oas_system.model.dao.system.StatusDao;
import com.lishangshan.oas_system.model.dao.system.TypeDao;
import com.lishangshan.oas_system.model.dao.user.DeptDao;
import com.lishangshan.oas_system.model.dao.user.UserDao;
import com.lishangshan.oas_system.service.inform.InformRelationService;
import com.lishangshan.oas_system.service.inform.InformService;
import com.lishangshan.oas_system.utils.ParameterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/")
public class InformListController {
    @Autowired
    private StatusDao statusDao;

    @Autowired
    private TypeDao typeDao;

    @Autowired
    private InformDao informDao;

    @Autowired
    private InformService informService;

    @Autowired
    private UserDao uDao;

    @Autowired
    private DeptDao deptDao;

    @Autowired
    private InformRelationDao informrelationDao;

    @Autowired
    private NoticeMapper noticeMapper;

    @Autowired
    private InformRelationService informrelationservice;

    @RequestMapping("infromlist")
    public String infromList(HttpSession session, HttpServletRequest req, Model model,
                             @RequestParam(value = "pageNum", defaultValue = "1") int page) {
        Long userId = Long.parseLong(session.getAttribute("userId") + "");
        PageHelper.startPage(page, ParameterUtil.PAGE_SIZE_MAX);
        List<Map<String, Object>> list = noticeMapper.findMyNotice(userId);
        PageInfo<Map<String, Object>> pageinfo = new PageInfo<Map<String, Object>>(list);
        List<Map<String, Object>> list2 = informrelationservice.setList(list);
      /*  for (Map<String, Object> map : list2) {
            System.out.println(map);
        }*/
        model.addAttribute("url", "informlistpaging");
        model.addAttribute("list", list2);
        model.addAttribute("page", pageinfo);
        System.out.println(pageinfo);
        return "inform/informlist";
    }

    /**
     * 通知列表的搜索/分页
     */
    @RequestMapping("informlistpaging")
    public String informListPaging(@RequestParam(value = "pageNum", defaultValue = "1") int page,
                                   @RequestParam(value = "baseKey", required = false) String baseKey,
                                   @RequestParam(value = "type", required = false) Integer type,
                                   @RequestParam(value = "status", required = false) Integer status,
                                   @RequestParam(value = "time", required = false) Integer time,
                                   @RequestParam(value = "icon", required = false) String icon,
                                   @SessionAttribute("userId") Long userId,
                                   Model model, HttpServletRequest req) {
        System.out.println("baseKey:" + baseKey);
        System.out.println("page:" + page);
        setSomething(baseKey, type, status, time, icon, model);
        PageHelper.startPage(page, 10);
        List<Map<String, Object>> list = noticeMapper.sortMyNotice(userId, baseKey, type, status, time);
        PageInfo<Map<String, Object>> pageinfo = new PageInfo<Map<String, Object>>(list);
        List<Map<String, Object>> list2 = informrelationservice.setList(list);
        for (Map<String, Object> map : list2) {
            System.out.println(map);
        }
        model.addAttribute("url", "informlistpaging");
        model.addAttribute("list", list2);
        model.addAttribute("page", pageinfo);
        return "inform/informlistpaging";

    }

    private void setSomething(String baseKey, Object type, Object status, Object time, Object icon, Model model) {
        if (!StringUtils.isEmpty(icon)) {
            model.addAttribute("icon", icon);
            if (!StringUtils.isEmpty(type)) {
                model.addAttribute("type", type);
                if ("1".equals(type)) {
                    model.addAttribute("sort", "&type=1&icon=" + icon);
                } else {
                    model.addAttribute("sort", "&type=0&icon=" + icon);
                }
            }
            if (!StringUtils.isEmpty(status)) {
                model.addAttribute("status", status);
                if ("1".equals(status)) {
                    model.addAttribute("sort", "&status=1&icon=" + icon);
                } else {
                    model.addAttribute("sort", "&status=0&icon=" + icon);
                }
            }
            if (!StringUtils.isEmpty(time)) {
                model.addAttribute("time", time);
                if ("1".equals(time)) {
                    model.addAttribute("sort", "&time=1&icon=" + icon);
                } else {
                    model.addAttribute("sort", "&time=0&icon=" + icon);
                }
            }
        }
        if (!StringUtils.isEmpty(baseKey)) {
            model.addAttribute("baseKey", baseKey);
        }
    }
}
